package com.wjm.serversocket.test2;

import com.wjm.serversocket.test1.UploadPicServer;

import java.io.File;

/**
 * 本地文件复制
 * 多线程复制
 */
public class ThreadCopyFile {
    public static void main(String[] args) {
        test();
    }

    private static void test() {

        //输出流
        File fileDir = new File(UploadPicServer.pcPf + File.separator + UploadPicServer.serverfilePath);//i:\\server
        if (fileDir.exists()) {
            File fileOut = new File(fileDir, UploadPicServer.fileName);
            if (fileOut.exists()) {
                fileOut.delete();
            }
        }


        File file = new File(UploadPicServer.fileStr);
        long lengthLong = file.length();
        int length = (int) file.length();
//        System.out.println("lengthLong==>" + lengthLong);
//        System.out.println("length==>" + length);

//        int s = 3;
//        int s = 1;
//        int s = 2;
        int s = 10;



//  从线程数得到每个线程要复制的数据大小
        int partLenghth = length / s + 1;
        for (int i = 0; i < s; i++) {
            int start = i * partLenghth;
            int end = (i + 1) * partLenghth - 1;
            if (i == length - 1) {
                end = length - 1;
            }
            MyRunnableCopyFile myRunnable = new MyRunnableCopyFile(start, end);
            Thread thread = new Thread(myRunnable);
            thread.start();
        }
        //单线程
//        Thread-0用时==》7s

        //多线程(有问题)
//        Thread-2用时==》3s
//        Thread-1用时==》5s
//        Thread-0用时==》7s

        //        多线程(修改后)线程数-2-
//        Thread-0-复制完成-lenCount-44072960
//        Thread-1用时==》4s
//        Thread-1-复制完成-lenCount-44061245

        //        多线程(修改后)线程数-3-计时不行
    }
}
